//
//  UIColor+More.m
//  HCBSYSTEM
//
//  Created by itte on 16/5/13.
//  Copyright © 2016年 itte. All rights reserved.
//

#import "UIColor+More.h"

@implementation UIColor (More)

+(UIColor *) colorwithHexString:(NSString *)hexString
{
    NSString *colorStr = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if (colorStr.length < 6) {
        return [UIColor clearColor];
    }
    if ([colorStr hasPrefix:@"0X"]) {
        colorStr = [colorStr substringFromIndex:2];
    }
    if ([colorStr hasPrefix:@"#"]) {
        colorStr = [colorStr substringFromIndex:1];
    }
    if (colorStr.length != 6) {
        return [UIColor clearColor];
    }
    NSRange range;
    range.location = 0;
    range.length = 2;
    // R
    NSString *rString = [colorStr substringWithRange:range];
    range.location = 2;
    // G
    NSString *gString = [colorStr substringWithRange:range];
    range.location = 4;
    // B
    NSString *bString = [colorStr substringWithRange:range];
    unsigned int r,g,b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float)r/255.0f) green:((float)g/255.0f) blue:((float)b/255.0f) alpha:1.0f];
}

@end
